//
//  StoreOrderDetailController.m
//  YongChuanTradingArea
//
//  Created by 李冰 on 2017/9/9.
//  Copyright © 2017年 李冰. All rights reserved.
//

#import "StoreOrderDetailController.h"
#import "OrderDetailListView.h"

@interface StoreOrderDetailController ()
@property (weak, nonatomic) IBOutlet UIImageView *coverImage;
@property (weak, nonatomic) IBOutlet UILabel *storeName;
@property (weak, nonatomic) IBOutlet UILabel *goodsIntro;
@property (weak, nonatomic) IBOutlet UILabel *currentPrice;
@property (weak, nonatomic) IBOutlet UILabel *originPrice;
@property (weak, nonatomic) IBOutlet UILabel *remark;
@property (weak, nonatomic) IBOutlet UILabel *typeName;
@property (weak, nonatomic) IBOutlet UILabel *validDate;
@property (weak, nonatomic) IBOutlet UILabel *goodsName;
@property (weak, nonatomic) IBOutlet UILabel *orderNumber;
@property (weak, nonatomic) IBOutlet UILabel *payDate;
@property (weak, nonatomic) IBOutlet UILabel *phoneNumber;
@property (weak, nonatomic) IBOutlet UILabel *goodsCount;
@property (weak, nonatomic) IBOutlet UILabel *totalPrice;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *listHeight;
@property (weak, nonatomic) IBOutlet UIView *listContent;

@end

@implementation StoreOrderDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"订单详情";
    
    self.view.backgroundColor = kBgColor;
    [self _updateUI];
}
- (void) _updateUI {

    [_coverImage sd_setImageWithURL:[NSURL URLWithString:self.model.Icon] placeholderImage:PLACE_HOLDER];
    _storeName.text = _model.MemberName;
//    _goodsIntro.text =  [NSString stringWithFormat:@"%@元%@", @"原价", [_model.BaseType integerValue] == 2 ? @"代金券一张" : @"团购套餐一份"];
    _goodsIntro.text =  [NSString stringWithFormat:@"%@ %@", _model.GoodsName, [_model.BaseType integerValue] == 2 ? @"代金券一张" : @"团购套餐一份"];
    _currentPrice.text = [NSString stringWithFormat:@"%.2f元", _model.UnitPrice.floatValue];
    _typeName.text = [_model.BaseType integerValue] == 2 ? @"代金券" : @"团购券";
    
    DecopnListModel *model = _model.DecopnlList.firstObject;
    _validDate.text = [NSString stringWithFormat:@"有效期至 %@", [NSDate timeStringWithTimestamp:[model.ValidityTime getTimestamp] formatterString:@"yyyy-MM-dd"]];
    
    
    for (UIView *sub in _listContent.subviews)
    {
        [sub removeFromSuperview];
    }
    CGFloat X = 0;
    CGFloat Y = 0;
    CGFloat W = lbScreenWidth;
    CGFloat H = 35;
    int i  = 0;
    for (DecopnListModel *model in _model.DecopnlList)
    {
        Y = i * H;
        OrderDetailListView *view = [[NSBundle mainBundle] loadNibNamed:@"OrderDetailListView" owner:nil options:nil].lastObject;
        view.frame = CGRectMake(X, Y, W, H);
        view.model = model;
        WEAKSELF;
        view.userBlock = ^(DecopnListModel *model) {
            
            [weakSelf userCoupon:model];
        };
        [_listContent addSubview:view];
        i++;
    }
    _listHeight.constant = _model.DecopnlList.count * H;
    
    _goodsName.text = _model.GoodsName;
    _orderNumber.text = _model.Ordersn;
    _payDate.text = [NSDate timeStringWithTimestamp:[_model.PayTime getTimestamp] formatterString:@"yyyy-MM-dd"];
    _phoneNumber.text = _model.PayPhone;
    _goodsCount.text = _model.PayNumber;
    
    _totalPrice.text = [NSString stringWithFormat:@"%.2f元", _model.TotalPrice.floatValue];
    
}

- (void) userCoupon:(DecopnListModel *) model {
    
    NSString *storeId = self.model.StoreId;
    NSString *couponNumber = model.DecopnNumeber;
    
    if (!storeId)
    {
        [MBProgressHUD showMessageWithOutIcon:@"店铺异常"];
        return;
    }
    
    if (couponNumber.length == 0)
    {
        [MBProgressHUD showMessageWithOutIcon:@"无可用商品券"];
        return;
    }
    
    [LBNetworkingInstance requestGET:inputCodeVerify parameters:NSDictionaryOfVariableBindings(storeId,couponNumber) showHUD:YES success:^(id  _Nonnull responseObject, NSInteger code, NSString * _Nonnull message) {
        
        if (code == YCNetworkingStatusCodeSuccess)
        {
            [self _updateData];
            NSString *message = [NSString stringWithFormat:@"成功验证1张消费券"];
            [UIAlertController showWithMessage:message controller:self sureAction:^{
                
            }];
        }
        
    } failure:^(NSError * _Nonnull err) {
        
    }];

}

- (IBAction)useAllAction:(UIButton *)sender {
    
    NSString *storeId = self.model.StoreId;
    NSMutableString *couponNumber = [NSMutableString new];
    
    NSInteger couponCount = 0;
    for (DecopnListModel *model in _model.DecopnlList)
    {
        if ([model.DecopnState isEqualToString:@"使用"])
        {
            if (couponNumber.length == 0)
            {
                [couponNumber appendString:model.DecopnNumeber];
            }
            else
            {
                [couponNumber appendFormat:@",%@", model.DecopnNumeber];
            }
            couponCount++;
        }
    }
    
    if (!storeId)
    {
        [MBProgressHUD showMessageWithOutIcon:@"店铺异常"];
        return;
    }
    
    if (couponNumber.length == 0)
    {
        [MBProgressHUD showMessageWithOutIcon:@"无可用商品券"];
        return;
    }
    
    [LBNetworkingInstance requestGET:inputCodeVerify parameters:NSDictionaryOfVariableBindings(storeId,couponNumber) showHUD:YES success:^(id  _Nonnull responseObject, NSInteger code, NSString * _Nonnull message) {

        if (code == YCNetworkingStatusCodeSuccess)
        {
            [self _updateData];
            NSString *message = [NSString stringWithFormat:@"成功验证%ld张消费券", couponCount];
            [UIAlertController showWithMessage:message controller:self sureAction:^{
                
            }];
        }

    } failure:^(NSError * _Nonnull err) {
        
    }];
}

- (void) _updateData {

    [LBNetworkingInstance requestGET:scanCodeVerify parameters:@{@"userPwd":self.QRCode} showHUD:YES success:^(id  _Nonnull responseObject, NSInteger code, NSString * _Nonnull message) {
        
        if (code == YCNetworkingStatusCodeSuccess)
        {
            StoreOrderDetailModel *model = [StoreOrderDetailModel yy_modelWithJSON:responseObject[@"Data"]];
            self.model = model;
            [self _updateUI];
        }
        
    } failure:^(NSError * _Nonnull err) {
        
    }];
}

- (UIStatusBarStyle)preferredStatusBarStyle {

    return UIStatusBarStyleDefault;
}

@end

@implementation StoreOrderDetailModel
+(NSDictionary<NSString *,id> *)modelContainerPropertyGenericClass {

    return @{@"DecopnlList":DecopnListModel.class};
}
@end

@implementation DecopnListModel

@end
